// MIT License
// 开源地址：https://gitee.com/co1024/AbcMvc
// Copyright (c) 2021-2023 1024
// Abc.Mvc=Furion+EF+SqlSugar+Pear layui admin.

namespace Abc.Web.Mvc.Areas.HtAdmin.Controllers;

[NonValidation]
//[AuthorizePolicy]
[AllowAnonymous]
[MaMvc("HtAdmin", controllerDescription: "数据字典", PCode = "HtAdmin_System")]
public class DataDictionaryController : HtAdminBaseController
{
    private readonly IDataConfigService _dataConfigService;
    private readonly IDataDictionaryService _dataDictionaryService;
    private readonly ILogger<DataDictionaryController> _logger;

    public DataDictionaryController(ILogger<DataDictionaryController> logger
        , IDataConfigService dataConfigService,
        IDataDictionaryService dataDictionaryService)
    {
        _logger = logger;
        _dataConfigService = dataConfigService;
        _dataDictionaryService = dataDictionaryService;
    }

    [HttpGet]
    [MaMenu("用户类型数据字典示例", Code = "Index", UrlPar = "?pkey=UserType")]
    [MaPermission("浏览", "Index,GetList")]
    public async Task<IActionResult> Index(string pkey = "")
    {
        ViewBag.PKey = pkey;
        return View();
    }

    [UnifyResult(typeof(DataConfigOut))]
    [HttpGet]
    [HttpPost]
    public async Task<JsonResult> GetList(DataDictionaryQuery searchquery)
    {
        searchquery.Limit = 9999;
        var pagedlist = await _dataDictionaryService.GetAllByPage(searchquery);
        return Json(pagedlist);
    }

    [MaPermission("详情", "Info")]
    public async Task<ActionResult> Add(string pkey)
    {
        return View("Info", new DataConfigOut { PKey = pkey });
    }

    [MaPermission("详情", "Info")]
    public async Task<ActionResult> Info(int? id, int dpid = 0)
    {
        ViewData["Dpid"] = dpid;
        DataDictionaryOut entity = null;
        if (id.HasValue) entity = await _dataConfigService.Get<DataDictionaryOut>(id.Value);
        return View(entity ?? new DataDictionaryOut());
    }

    [HttpPost]
    // [ValidateInput(false)]
    [MaPermission("编辑", "Edit")]
    public async Task<JsonResult> Edit(DataDictionaryInput input)
    {
        await _dataDictionaryService.InsertOrUpdate(input);
        return Json("");
    }

    [HttpPost]
    [MaPermission("删除", "Delete")]
    public async Task<JsonResult> Delete(int id)
    {
        await _dataConfigService.Delete(id);
        return Json("");
    }

    /// <summary>
    ///     批量删除
    /// </summary>
    /// <param name="ids">需要删除数据，多个英文逗号隔开</param>
    /// <returns></returns>
    [HttpPost]
    [MaPermission("批量删除", "BatchDelete")]
    public async Task<JsonResult> BatchDelete(string ids)
    {
        await _dataConfigService.BatchDelete(ids);
        return Json("");
    }
}